//
//  UIViewController+YTExtension.m
//  YiTuUserClient
//
//  Created by 石少庸 on 2017/3/8.
//  Copyright © 2017年 YiTu. All rights reserved.
//

#import "UIViewController+YTExtension.h"
#import "UIImage+YTExtension.h"

@implementation UIViewController (YTExtension)

- (void)yt_changeNavigationColor:(UIColor *)color
             deleteBottomLine:(BOOL)deleteLine {
    [self.navigationController.navigationBar setBackgroundImage:[UIImage yt_imageWithColor:color]
                                                  forBarMetrics:0];
    if (deleteLine) {
        self.navigationController.navigationBar.shadowImage = [UIImage new];
    } else {
        self.navigationController.navigationBar.shadowImage = nil;
    }
}

- (void)yt_changStatusBarWithStyle:(UIStatusBarStyle)style {
    [[UIApplication sharedApplication] setStatusBarStyle:style
                                                animated:NO];
}

- (void)yt_changeNavigationBarWithMainColor {
    [self yt_changeNavigationColor:kMainBlueColor deleteBottomLine:NO];
    [self yt_changStatusBarWithStyle:UIStatusBarStyleLightContent];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)yt_changeNavigationBarWithWhiteColor {
    [self yt_changeNavigationColor:[UIColor whiteColor] deleteBottomLine:NO];
    [self yt_changStatusBarWithStyle:UIStatusBarStyleDefault];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
}

- (void)yt_changeNavigationClearColor {
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    UIImage *image = [UIImage yt_imageWithColor:[UIColor whiteColor] alpha:0.0];
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

- (UIBarButtonItem *)yt_changeLeftBarItemWithImage:(NSString *)image
                                            target:(id)target
                                          selector:(SEL)selector {
    return [self yt_changeLeftBarItemWithImage:image
                                        target:target
                                    titleColor:kMainBlueColor
                                      selector:selector];
}

- (UIBarButtonItem *)yt_changeLeftBarItemWithImage:(NSString *)image
                                            target:(id)target
                                        titleColor:(UIColor *)titleColor
                                          selector:(SEL)selector {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@" 返回" forState:UIControlStateNormal];
    [button setTitleColor:titleColor forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 55, 44);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
    if (image) {
        [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    } else {
        [button setImage:[UIImage imageNamed:@"public_back_arrow"] forState:UIControlStateNormal];
    }
    [button addTarget:target
               action:selector
     forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}

- (UIBarButtonItem *)yt_changeRightBarItemWithImage:(NSString *)image
                                             target:(id)target
                                              title:(NSString *)title
                                           selector:(SEL)selector {
    UIBarButtonItem *right = [[UIBarButtonItem alloc] init];
    [right setTarget:target];
    [right setAction:selector];
    if (image) {
        [right setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    
    if (title) {
        [right setTitle:title];
    }
    return right;
}

- (void)yt_selecteImageCompletion:(void (^)(UIImage * _Nullable))completion {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"照片选取" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self selectPicFrom:UIImagePickerControllerSourceTypePhotoLibrary completion:completion];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self selectPicFrom:UIImagePickerControllerSourceTypeCamera completion:completion];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)selectPicFrom:(UIImagePickerControllerSourceType)type completion:(void (^)(UIImage * _Nullable))completion {
    if ([UIImagePickerController isSourceTypeAvailable:type]) {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.sourceType = type;
        picker.bk_didFinishPickingMediaBlock = ^(UIImagePickerController *picker, NSDictionary *userInfo) {
            [picker dismissViewControllerAnimated:YES completion:nil];
            UIImage *image = userInfo[UIImagePickerControllerOriginalImage];
            if (completion) {
                completion(image);
            }
        };
        picker.bk_didCancelBlock = ^(UIImagePickerController *picker) {
            [picker dismissViewControllerAnimated:YES completion:nil];
        };
        [self presentViewController:picker animated:nil completion:nil];
    }
}

- (ShowHUD)yt_showHudWithTips:(NSString *)tips {
    return ^(NSNumber *executing) {
        if (executing.boolValue) {
            [SVProgressHUD showWithStatus:tips];
        } else {
            [SVProgressHUD dismiss];
        }
    };
}

@end
